<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Admin
 *
 * @author Massri
 */
class Application_Model_Approval{
    //put your code here
    
    private $_db;

    public $_loops;
    
    function __construct() {
        $registry = Zend_Registry::getInstance();
        $this->_db = $registry->database;
    }

    function getOrderWithdraw($start, $limit) {

        $sql = "select FORMAT(count(1),0) as total, FORMAT(sum(amt1),0) as jumlah
                from v_transaction where trans_type_code = 'TX02' and trans_status = 'APPROVAL'";

        $result = $this->_db->fetchRow($sql);
        $rows = $result['total'];
        $total = $result['jumlah'];

        $data = array($rows, $total);

       
        $sql = "select
                trans_dtm, acc_num1, concat('[',bank_account_no,'] ',bank_name,' (',account_name,')') as acc_num2,
                FORMAT(amt1,0) as amt_rp, amt1, notes_txt as descr, trans_type_id,
                CONCAT('<a href=\"".Zend_Controller_Front::getInstance()->getBaseUrl()."/approval/followupwithdraw/id/', trans_header_id, '\"><img src=\"".Zend_Controller_Front::getInstance()->getBaseUrl()."/jeasy/themes/gray/icons/redo2.png\"></a>') as follow
                from v_transaction where trans_type_code = 'TX02' and trans_status = 'APPROVAL'
                order by trans_header_id
                limit $start, $limit";

        $result = $this->_db->fetchAll($sql);


        array_push($data, $result);

       
        return $data;
    }

    function detailWithdraw($id){

        

        $sql = "select
                trans_header_id, trans_dtm, acc_num1, acc_num2,
                bank_account_no, bank_name, account_name as member_name,
                FORMAT(amt1,0) as amt_rp, amt1, notes_txt as descr,
                trans_type_id
                from v_transaction where trans_header_id = $id and trans_status = 'APPROVAL' ";

        $result = $this->_db->fetchRow($sql);

        return $result;

    }


    
    function prosesWithdraw($id, $flag){
        $this->_db->beginTransaction();

        $result = array();

        try {

            $dataInput = array($id, $flag);

            
            $stmt = $this->_db->query("CALL Trx_WithdrawFollowup( ?, ?, @out_code, @out_msg)", $dataInput);
            $stmt = $this->_db->query("SELECT @out_code as out_code, @out_msg as out_msg");
            
            $data = $stmt->fetchAll();

            $code = $data[0]['out_code'];
            $msg = $data[0]['out_msg'].' di '.$flag;
            
            if($code == 1){

                $this->_db->rollBack();

                $result = array(
                    'code'  => (string)$code,
                    'msg'   => $msg
                );

                return $result;

            }

            $this->_db->commit();


            $result = array(
                'code'  => (string)$code,
                'msg'   => $msg
            );



            return $result;


        }catch (Exception $e)
        {
            $this->_db->rollBack();
            //my_error_handlier($e);
            $arrE = explode(":", $e);
            $arrE2 = explode(" in ", $arrE[1]);
            $errMsg = $arrE2[0];

            $result = array(
                'code'  => 1,
                'msg'   => $errMsg,
                'trans_hid'  => $id
            );

            return $result;
        }
    }


    function getOrderTopupPoint($start, $limit) {

        $sql = "select FORMAT(count(1),0) as total, FORMAT(sum(amt1),0) as jumlah
                from v_transaction where trans_type_code = 'TX03' and trans_status = 'APPROVAL'";

        $result = $this->_db->fetchRow($sql);
        $rows = $result['total'];
        $total = $result['jumlah'];

        $data = array($rows, $total);


        $sql = "select
                trans_dtm, acc_num1, concat('[',bank_account_no,'] ',bank_name,' (',account_name,')') as acc_num2,
                FORMAT(amt1,0) as amt_rp, amt1, notes_txt as descr, trans_type_id,
                SUBSTRING_INDEX(SUBSTRING_INDEX(external_param, '#', 1), '#', -1) AS transfer_dat,
                SUBSTRING_INDEX(SUBSTRING_INDEX(external_param, '#', 2), '#', -1) AS ket,
                CONCAT('<a href=\"".Zend_Controller_Front::getInstance()->getBaseUrl()."/approval/followuptopuppoint/id/', trans_header_id, '\"><img src=\"".Zend_Controller_Front::getInstance()->getBaseUrl()."/jeasy/themes/gray/icons/redo2.png\"></a>') as follow
                from v_transaction where trans_type_code = 'TX03' and trans_status = 'APPROVAL'
                order by trans_header_id
                limit $start, $limit";

        $result = $this->_db->fetchAll($sql);


        array_push($data, $result);


        return $data;
    }

    function detailTopupPoint($id){



        $sql = "select
                trans_header_id, trans_dtm, acc_num1, acc_num2,
                bank_account_no, bank_name, account_name as member_name,
                FORMAT(amt1,0) as amt_rp, amt1, notes_txt as descr,
                trans_type_id,
                SUBSTRING_INDEX(SUBSTRING_INDEX(external_param, '#', 1), '#', -1) AS transfer_dat,
                SUBSTRING_INDEX(SUBSTRING_INDEX(external_param, '#', 2), '#', -1) AS ket
                from v_transaction where trans_header_id = $id and trans_status = 'APPROVAL' ";

        $result = $this->_db->fetchRow($sql);

        return $result;

    }


    function prosesTopup($id, $flag){
        $this->_db->beginTransaction();

        $result = array();

        try {

            $dataInput = array($id, $flag);


            $stmt = $this->_db->query("CALL Trx_TopupFollowup( ?, ?, @out_code, @out_msg)", $dataInput);
            $stmt = $this->_db->query("SELECT @out_code as out_code, @out_msg as out_msg");

            $data = $stmt->fetchAll();

            $code = $data[0]['out_code'];
            $msg = $data[0]['out_msg'].' di '.$flag;

            if($code == 1){

                $this->_db->rollBack();

                $result = array(
                    'code'  => (string)$code,
                    'msg'   => $msg
                );

                return $result;

            }

            $this->_db->commit();


            $result = array(
                'code'  => (string)$code,
                'msg'   => $msg
            );



            return $result;


        }catch (Exception $e)
        {
            $this->_db->rollBack();
            //my_error_handlier($e);
            $arrE = explode(":", $e);
            $arrE2 = explode(" in ", $arrE[1]);
            $errMsg = $arrE2[0];

            $result = array(
                'code'  => 1,
                'msg'   => $errMsg,
                'trans_hid'  => $id
            );

            return $result;
        }
    }

}

?>
